package com.xb.loan.fund.common;

import com.xb.loan.account.service.bean.CashInfoVO;
import com.xb.loan.account.service.bean.CouponInfoVO;
import com.xb.loan.account.service.bean.LotCashInfoVO;
import com.xb.loan.account.service.constant.AccountConst;
import com.xb.loan.account.service.exception.IAccountServiceException;
import com.xb.loan.log.exception.BizException;
import com.xb.loan.log.exception.IBizException;
import com.xb.loan.usercenter.service.user.bean.UserInfoVO;
import com.xb.loan.usercenter.service.user.constant.UserConst;
import com.xb.loan.usercenter.service.user.exception.IUserServiceException;
import com.xb.loan.util.log.DefaultLoggerFacotry;
import org.slf4j.Logger;

public class VerifySet {
	private static Logger logger = DefaultLoggerFacotry.getLogger(VerifySet.class);
	public static String getSourcePosition() {
		Exception e = new RuntimeException();
		StackTraceElement element = e.getStackTrace()[2];
		StringBuffer buffer = new StringBuffer();
		buffer.append("class=").append(element.getClassName()).append("\t")
			.append("method=").append(element.getMethodName()).append("\t");
		return buffer.toString();
	}
	
	/**
	 * 验证用户ID是否为null
	 * @param userId 用户ID
	 * @throws BizException
	 */
	public static void verifyUserId(String userId) throws BizException {
		if(userId == null){
			StringBuffer buffer = new StringBuffer();
			buffer.append(getSourcePosition()).append("userId=").append(userId)
			.append("\t").append(IBizException.inputParameterIsWrong.getMessage());
			logger.warn(buffer.toString());
			throw IBizException.inputParameterIsWrong;
		}
	}
	
	/**
	 * 验证用户信息是否为null
	 * @param userInfoVO 用户信息
	 * @throws BizException
	 * 2015年9月13日 by wenju.song
	 */
	public static void verifyUserInfoVO(UserInfoVO userInfoVO) throws BizException{
		if(userInfoVO == null){
			StringBuffer buffer = new StringBuffer();
			buffer.append(getSourcePosition()).append("userInfoVO=").append(userInfoVO)
			.append("\t").append(IBizException.inputParameterIsWrong.getMessage());
			logger.warn(buffer.toString());
			throw IBizException.inputParameterIsWrong;
		}
	}

	/**
	 * 验证用户是否处于正常状态
	 * @param userInfoVO 用户信息
	 * @throws BizException
	 */
	public static void verifyUserInfoVOIsNormal(UserInfoVO userInfoVO) throws BizException{
		if(userInfoVO == null){
			StringBuffer buffer = new StringBuffer();
			buffer.append(getSourcePosition()).append("userInfoVO=").append(userInfoVO)
			.append("\t").append(IUserServiceException.userInfoMissing.getMessage());
			logger.warn(buffer.toString());
			throw IUserServiceException.userInfoMissing;
		}
		if (userInfoVO.getStatus() != UserConst.UserStatus.NORMALSTATUS.getCode()){
			StringBuffer buffer = new StringBuffer();
			buffer.append(getSourcePosition()).append("userId=").append(userInfoVO.getUserId())
			.append("\t").append(IUserServiceException.userStatusNotNormal.getMessage());
			logger.warn(buffer.toString());
			throw IUserServiceException.userStatusNotNormal;
		}
	}



	/**
	 * 验证金额不能为负值
	 * @param money
	 * @throws BizException
	 */
	public static void verifyMoney(double money, String foreignId) throws BizException {
		if (money <= 0) {
			StringBuffer buffer = new StringBuffer();
			buffer.append(getSourcePosition()).append("money=").append(money).append("\tforeignId=").append(foreignId)
			.append("\t").append(IAccountServiceException.moneyNotPositive.getMessage());
			logger.warn(buffer.toString());
			throw IAccountServiceException.moneyNotPositive;
		}
	}
	
	/**
	 * 验证账户ID是否为null
	 * @param accountId 账户ID
	 * @throws BizException
	 */
	public static void verifyAccountId(String accountId) throws BizException{
		if(accountId == null){
			StringBuffer buffer = new StringBuffer();
			buffer.append(getSourcePosition()).append("accountId=").append(accountId)
			.append("\t").append(IBizException.inputParameterIsWrong.getMessage());
			logger.warn(buffer.toString());
			throw IBizException.inputParameterIsWrong;
		}
	}
	
	/**
	 * 验证现金账户是否处于正常状态
	 * @param cashInfoVO 现金账户信息
	 * @throws BizException
	 */
	public static void verifyCashInfoVOIsNormal(CashInfoVO cashInfoVO) throws BizException{
		if(cashInfoVO == null){
			StringBuffer buffer = new StringBuffer();
			buffer.append(getSourcePosition()).append("cashInfoVO=").append(cashInfoVO)
			.append("\t").append(IBizException.inputParameterIsWrong.getMessage());
			logger.warn(buffer.toString());
			throw IBizException.inputParameterIsWrong;
		}
		if(cashInfoVO.getStatus() != AccountConst.AccountStatus_Normal){
			StringBuffer buffer = new StringBuffer();
			buffer.append(getSourcePosition()).append("accountId=").append(cashInfoVO.getAccountId())
			.append("\t").append(IAccountServiceException.accountNotNormal.getMessage());
			logger.warn(buffer.toString());
			throw IAccountServiceException.accountNotNormal;
		}
	}
	
	/**
	 * 验证彩金账户是否为null
	 * @param lotCashInfoVO 彩金账户信息
	 * @throws BizException
	 */
	public static void verifyLotCashInfoVO(LotCashInfoVO lotCashInfoVO) throws BizException{
		if(lotCashInfoVO == null){
			StringBuffer buffer = new StringBuffer();
			buffer.append(getSourcePosition()).append("lotCashInfoVO=").append(lotCashInfoVO)
			.append("\t").append(IBizException.inputParameterIsWrong.getMessage());
			logger.warn(buffer.toString());
			throw IBizException.inputParameterIsWrong;
		}
	}
	
	/**
	 * 验证couponId是否为null
	 * @param couponId 礼券账户ID
	 * @throws BizException
	 */
	public static void verifyCouponId(String couponId) throws BizException{
		if(couponId == null || couponId.equals("")){
			StringBuffer buffer = new StringBuffer();
			buffer.append(getSourcePosition()).append("couponId=").append(couponId)
			.append("\t").append(IBizException.inputParameterIsWrong.getMessage());
			logger.warn(buffer.toString());
			throw IBizException.inputParameterIsWrong;
		}
	}
	
	/**
	 * 验证礼券账户是否为null
	 * @param couponInfoVO 礼券账户信息
	 * @throws BizException
	 */
	public static void verifyCouponInfoVO(CouponInfoVO couponInfoVO) throws BizException{
		if(couponInfoVO == null){
			StringBuffer buffer = new StringBuffer();
			buffer.append(getSourcePosition()).append("couponInfoVO=").append(couponInfoVO)
			.append("\t").append(IBizException.inputParameterIsWrong.getMessage());
			logger.warn(buffer.toString());
			throw IBizException.inputParameterIsWrong;
		}
	}

	
	public static void main(String[] args) throws BizException {
		verifyUserId(null);
	}
}
